package com.gaeainfo.modules.alarm.collection.service;

import com.gaeainfo.constant.GaeaConstant;
import com.gaeainfo.framework.core.utils.common.GaeaCommonUtil;
import com.gaeainfo.modules.alarm.collection.dao.AlarmCollectionDao;
import com.gaeainfo.modules.alarm.collection.dto.PersonDto;
import com.gaeainfo.modules.alarm.collection.dto.VehicleDto;
import com.gaeainfo.modules.alarm.collection.entity.AlarmPersonEntity;
import com.gaeainfo.modules.alarm.collection.entity.AlarmVehicleEntity;
import com.gaeainfo.modules.alarm.collection.entity.PersonEntity;
import com.gaeainfo.modules.alarm.collection.entity.VehicleEntity;
import com.gaeainfo.modules.base.adapter.CollectionAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * Created by Caven on 2018/7/13.
 */

@Service("alarmCollectionService")
public class AlarmCollectionServiceImpl implements AlarmCollectionService {

    @Resource(name = "alarmCollectionAdapter")
    private CollectionAdapter alarmCollectionAdapter;

    @Autowired
    private AlarmCollectionDao alarmCollectionDao;

    @Override
    public PersonDto getPersonInfo(String idCard, String phone) throws Exception {
        PersonDto personDto = null;
        PersonEntity personEntity = null;
        if (!GaeaCommonUtil.isNullOrEmpty(idCard)) {
            personDto = (PersonDto) alarmCollectionAdapter.gerPersonByIdCard(idCard);
            personEntity = alarmCollectionDao.findPersonByIdCard(idCard);
        } else if (!GaeaCommonUtil.isNullOrEmpty(phone)) {
            personDto = (PersonDto) alarmCollectionAdapter.gerPersonByPhone(phone);
            personEntity = alarmCollectionDao.findPersonByPhone(phone);
        }
        if (personDto != null && personEntity == null) {
            personEntity = new PersonEntity();
            personEntity.setPersonId(GaeaCommonUtil.generateUUID(""));
            personEntity.setName(personDto.getName());
            personEntity.setIdCard(personDto.getIdCard());
            personEntity.setPhone(personDto.getPhone());
            personEntity.setEthnic(personDto.getEthnic());
            personEntity.setAge(personDto.getAge());
            personEntity.setSex(personDto.getSex());
            personEntity.setResidenceAddr(personDto.getResidenceAddr());
            personEntity.setTemporarilyAddr(personDto.getTemporarilyAddr());
            personEntity.setFocus(personDto.isFocus() ? "1" : "0");
            personEntity.setFugitive(personDto.isFugitive() ? "1" : "0");
            alarmCollectionDao.savePerson(personEntity);
        } else if (personDto != null && personEntity != null) {
            personEntity.setPhone(personDto.getPhone());
            personEntity.setTemporarilyAddr(personDto.getTemporarilyAddr());
            personEntity.setFocus(personDto.isFocus() ? "1" : "0");
            personEntity.setFugitive(personDto.isFugitive() ? "1" : "0");
            alarmCollectionDao.updatePerson(personEntity);
        } else if (personDto == null && personEntity != null) {
            personDto = new PersonDto();
            personDto.setPersonId(personEntity.getPersonId());
            personDto.setName(personEntity.getName());
            personDto.setIdCard(personEntity.getIdCard());
            personDto.setPhone(personEntity.getPhone());
            personDto.setEthnic(personEntity.getEthnic());
            personDto.setAge(personEntity.getAge());
            personDto.setSex(personEntity.getSex());
            personDto.setResidenceAddr(personEntity.getResidenceAddr());
            personDto.setTemporarilyAddr(personEntity.getTemporarilyAddr());
            personDto.setFocus("1".equals(personEntity.getFocus()));
            personDto.setFugitive("1".equals(personEntity.getFugitive()));
        }

        return personDto;
    }

    @Override
    public String saveAlarmPerson(String alarmId, String personId, String personType) throws Exception {
        AlarmPersonEntity alarmPersonEntity = new AlarmPersonEntity();
        alarmPersonEntity.setPid(GaeaCommonUtil.generateUUID(""));
        alarmPersonEntity.setAlarmId(alarmId);
        alarmPersonEntity.setPersonId(personId);
        alarmPersonEntity.setPersonType(personType);
        alarmPersonEntity.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        alarmCollectionDao.saveAlarmPerson(alarmPersonEntity);
        return GaeaConstant.Result.SUCCESS;
    }

    @Override
    public List<PersonDto> getAlarmPerson(String alarmId) throws Exception {
        List<PersonDto> personList = new ArrayList<>();
        List<Map<String, Object>> result = alarmCollectionDao.findAlarmPersonByAlarmId(alarmId);
        if (result != null && !result.isEmpty()) {
            for (Map<String, Object> rowData : result) {
                PersonDto personDto = new PersonDto();
                personDto.setPersonId(GaeaCommonUtil.trimToEmpty(rowData.get("PERSON_ID")));
                personDto.setName(GaeaCommonUtil.trimToEmpty(rowData.get("NAME")));
                personDto.setIdCard(GaeaCommonUtil.trimToEmpty(rowData.get("ID_CARD")));
                personDto.setPhone(GaeaCommonUtil.trimToEmpty(rowData.get("PHONE")));
                personDto.setEthnic(GaeaCommonUtil.trimToEmpty(rowData.get("ETHNIC")));
                personDto.setAge(Integer.parseInt(GaeaCommonUtil.trimToEmpty(rowData.get("AGE"))));
                personDto.setSex(GaeaCommonUtil.trimToEmpty(rowData.get("SEX")));
                personDto.setResidenceAddr(GaeaCommonUtil.trimToEmpty(rowData.get("RESIDENCE_ADDRESS")));
                personDto.setTemporarilyAddr(GaeaCommonUtil.trimToEmpty(rowData.get("TEMPORARILY_ADDRESS")));
                personDto.setFocus("1".equals(GaeaCommonUtil.trimToEmpty(rowData.get("IS_FOCUS"))));
                personDto.setFugitive("1".equals(GaeaCommonUtil.trimToEmpty(rowData.get("IS_FUGITIVE"))));
                personDto.setPersonType(GaeaCommonUtil.trimToEmpty(rowData.get("PERSON_TYPE")));
                personDto.setCriminal("1".equals(GaeaCommonUtil.trimToEmpty(rowData.get("IS_CRIMINAL"))));
                personDto.setCriminalRecord(GaeaCommonUtil.trimToEmpty(rowData.get("CRIMINAL_RECORD")));
                personList.add(personDto);
            }
        }
        return personList;
    }

    @Override
    public String deleteAlarmPerson(String alarmId, String personId) throws Exception {
        AlarmPersonEntity alarmPersonEntity = alarmCollectionDao.findAlarmPersonById(alarmId, personId);
        alarmCollectionDao.deleteAlarmPerson(alarmPersonEntity);
        return GaeaConstant.Result.SUCCESS;
    }

    @Override
    public VehicleDto getVehicleInfo(String plateNo) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-DD");
        VehicleDto vehicleDto = (VehicleDto) alarmCollectionAdapter.getVehicleByPlateNo(plateNo);
        VehicleEntity vehicleEntity = alarmCollectionDao.findVehicleByPlateNo(plateNo);
        if (vehicleDto != null && vehicleEntity == null) {
            vehicleEntity = new VehicleEntity();
            vehicleEntity.setVehicleId(GaeaCommonUtil.generateUUID(""));
            vehicleEntity.setPlateNo(vehicleDto.getPlateNo());
            vehicleEntity.setType(vehicleDto.getType());
            vehicleEntity.setBrand(vehicleDto.getBrand());
            vehicleEntity.setColor(vehicleDto.getColor());
            vehicleEntity.setUse(vehicleDto.getUse());
            vehicleEntity.setVin(vehicleDto.getVin());
            vehicleEntity.setManufacture(vehicleDto.getManufacture());
            vehicleEntity.setOwnerName(vehicleDto.getOwnerName());
            vehicleEntity.setOwnerAddr(vehicleDto.getOwnerAddr());
            vehicleEntity.setOwnerPhone(vehicleDto.getOwnerPhone());
            vehicleEntity.setRegion(vehicleDto.getRegion());
            if (!GaeaCommonUtil.isNullOrEmpty(vehicleDto.getRegisterDate())) {
                vehicleEntity.setRegisterDate(new Timestamp(df.parse(vehicleDto.getRegisterDate()).getTime()));
            }
            alarmCollectionDao.saveVehicle(vehicleEntity);
        } else if (vehicleDto == null && vehicleEntity != null) {
            vehicleDto = new VehicleDto();
            vehicleDto.setVehicleId(vehicleEntity.getVehicleId());
            vehicleDto.setPlateNo(vehicleEntity.getPlateNo());
            vehicleDto.setType(vehicleEntity.getType());
            vehicleDto.setBrand(vehicleEntity.getBrand());
            vehicleDto.setColor(vehicleEntity.getColor());
            vehicleDto.setUse(vehicleEntity.getUse());
            vehicleDto.setVin(vehicleEntity.getVin());
            vehicleDto.setManufacture(vehicleEntity.getManufacture());
            vehicleDto.setOwnerName(vehicleEntity.getOwnerName());
            vehicleDto.setOwnerAddr(vehicleEntity.getOwnerAddr());
            vehicleDto.setOwnerPhone(vehicleEntity.getOwnerPhone());
            vehicleDto.setRegion(vehicleEntity.getRegion());
            if (!GaeaCommonUtil.isNullOrEmpty(vehicleEntity.getRegisterDate())) {
                vehicleDto.setRegisterDate(df.format(vehicleEntity.getRegisterDate()));
            }
        }
        return vehicleDto;
    }

    @Override
    public String saveAlarmVehicle(String alarmId, String vehicleId) throws Exception {
        AlarmVehicleEntity alarmVehicleEntity = new AlarmVehicleEntity();
        alarmVehicleEntity.setPid(GaeaCommonUtil.generateUUID(""));
        alarmVehicleEntity.setAlarmId(alarmId);
        alarmVehicleEntity.setVehicleId(vehicleId);
        alarmVehicleEntity.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        alarmCollectionDao.saveAlarmVehicle(alarmVehicleEntity);
        return GaeaConstant.Result.SUCCESS;
    }

    @Override
    public List<VehicleDto> getAlarmVehicle(String alarmId) throws Exception {
        List<VehicleDto> vehicleList = new ArrayList<>();
        List<Map<String, Object>> result = alarmCollectionDao.findAlarmPersonByAlarmId(alarmId);
        if (result != null && !result.isEmpty()) {
            for (Map<String, Object> rowData : result) {
                VehicleDto vehicleDto = new VehicleDto();
                vehicleDto.setVehicleId(GaeaCommonUtil.trimToEmpty(rowData.get("VEHICLE_ID")));
                vehicleDto.setPlateNo(GaeaCommonUtil.trimToEmpty(rowData.get("PLATE_NO")));
                vehicleDto.setType(GaeaCommonUtil.trimToEmpty(rowData.get("TYPE")));
                vehicleDto.setBrand(GaeaCommonUtil.trimToEmpty(rowData.get("BRAND")));
                vehicleDto.setColor(GaeaCommonUtil.trimToEmpty(rowData.get("COLOR")));
                vehicleDto.setUse(GaeaCommonUtil.trimToEmpty(rowData.get("USE")));
                vehicleDto.setVin(GaeaCommonUtil.trimToEmpty(rowData.get("VIN")));
                vehicleDto.setManufacture(GaeaCommonUtil.trimToEmpty(rowData.get("MANUFACTURE")));
                vehicleDto.setOwnerName(GaeaCommonUtil.trimToEmpty(rowData.get("OWNER_NAME")));
                vehicleDto.setOwnerAddr(GaeaCommonUtil.trimToEmpty(rowData.get("OWNER_ADDRESS")));
                vehicleDto.setOwnerPhone(GaeaCommonUtil.trimToEmpty(rowData.get("OWNER_PHONE")));
                vehicleDto.setRegion(GaeaCommonUtil.trimToEmpty(rowData.get("REGION")));
                vehicleDto.setRegisterDate(GaeaCommonUtil.trimToEmpty(rowData.get("REGISTER_DATE")));
                vehicleList.add(vehicleDto);
            }
        }
        return vehicleList;
    }

    @Override
    public String deleteAlarmVehicle(String alarmId, String vehicleId) throws Exception {
        AlarmVehicleEntity alarmVehicleEntity = alarmCollectionDao.findAlarmVehicleById(alarmId, vehicleId);
        alarmCollectionDao.deleteAlarmVehicle(alarmVehicleEntity);
        return GaeaConstant.Result.SUCCESS;
    }


}
